package com.prism.moodme.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Random;

import javax.swing.JPanel;

import com.prism.moodme.app.SearchPanel;
import com.prism.moodme.data.Data;
import com.prism.moodme.datastore.AccessDataStore;

public class Utility {
	public static String path = "F:\\data";
	public static Random random = new Random(System.currentTimeMillis());
	public static final int MAPHeight = 500;
	public static final int MAPWidth = 400;
	public static final int CHARTHeight = 500;
	public static final int CHARTWidth = 300;
	public static final String[] _MONTHS =
		new String[] {"MONTH","January","February","March",
						"April","May","June",
						"July","August","September",
						"October","November","December"};
	public static final String[] _DAYS =
    	new String[] {"DAY","1","2","3","4","5","6","7","8","9",
						"10","11","12","13","14","15","16","17",
						"18","19","20","21","22","23","24","25",
						"26","27","28","29","30","31"};
	public static final String[] _HOURS =
    	new String[]{"HOUR","1","2","3","4","5","6","7","8","9",
    					"10","11","12","13","14","15","16",
    					"17","18","19","20","21","22","23","0"};

	public static final String[] _MINUTES =
    	new String[] {"MINUTE","0","5","10","15","20","25","30",
    					"35","40","45","50","55","60"};

	public static int getMonth(String month){
		for(int i=0; i< _MONTHS.length; i++)
			if(_MONTHS[i].equalsIgnoreCase(month))
					return i;
		return 0;
	}

	public static int getDay(String day){
		for(int i=0; i< _DAYS.length; i++)
			if(_DAYS[i].equalsIgnoreCase(day))
					return i;
		return 0;
	}

	public static int getHour(String hour){
		for(int i=0; i< _HOURS.length; i++)
			if(_HOURS[i].equalsIgnoreCase(hour))
					return Integer.parseInt(_HOURS[i]);
		return 0;
	}


	public static int getMin(String min){
		for(int i=0; i< _MINUTES.length; i++)
			if(_MINUTES[i].equalsIgnoreCase(min))
					return Integer.parseInt(_MINUTES[i]);
		return 0;
	}

	public static ArrayList<String> readContent(String path){
		ArrayList<String> content = new ArrayList<String>();
		File index = new File(path);
		try{
			BufferedReader in = new BufferedReader(new FileReader(index));
			String line;
			while((line = in.readLine()) != null){
				content.add(line);
			}
			in.close();
		}catch(IOException e){
			System.out.println(e);
		}
		return content;
	}

	public static List<Data> getData(String query){

		long timeStamp = getTime(query);
//		System.out.println(query);
//		System.out.println(Utility.getTime(timeStamp).getTime());
//		ArrayList<String> points = Utility.readContent(path);
		try {

			Data event = AccessDataStore.getData("1", timeStamp);
			if(event != null){
				ArrayList<Data> data = AccessDataStore.getData("1", event.getCondition());

				if(data.size() > 200)
					return data.subList(0, 200);
				return data;
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();

		}
		return null;
	}

	public static String searchAction(SearchPanel searchpanel){
		StringBuilder sb = new StringBuilder();
		sb.append(searchpanel.month);sb.append("\t");
		sb.append(searchpanel.day);sb.append("\t");
		sb.append(searchpanel.hour);sb.append("\t");
		sb.append(searchpanel.min);
		return sb.toString();
	}

	public static Calendar getTime(long timeStamp){
		Calendar calendar = new GregorianCalendar();
		calendar.setTimeInMillis(timeStamp);
		return calendar;
	}

	public static String getMap(List<Data> _data){
	//		List<Data> _data = getData();
			//TODO getData from Database.
			String map = DisplayUtility.getMapURL(_data, MAPHeight, MAPWidth);
			return map;
		}

	public static String getDayChart(List<Data> _data) {
		//		List<Data> _data = getData();
		//TODO getData from Database.

		String dayChart = DisplayUtility.getDayChartURL(_data, CHARTHeight, CHARTWidth);
		return dayChart;
	}

	public static String getHourChart(List<Data> _data) {
		//		List<Data> _data = getData();
		//TODO getData from Database.

		String hourChart = DisplayUtility.getTimeChartURL(_data, CHARTHeight, CHARTWidth);
		return hourChart;
	}

	public static long getTime(String query){
		//time = YYYY\tMONTH\tDAY\t24HR\tMIN
		String[] split = query.split("\t");
		Calendar calendar = new GregorianCalendar(Integer.parseInt(split[0]),
													Integer.parseInt(split[1]),
													Integer.parseInt(split[2]),
													Integer.parseInt(split[3]),
													Integer.parseInt(split[4]));

		return calendar.getTimeInMillis();
	}
}
